import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;

public class ChannelNioExample {

    public static void main(String[] args) {
        //read1();
        //read2();
        write1();
        //write2();
        //write3();
        //copy();
    }


    private static void read1()
    {
        try(FileChannel seekableByteChannel = (FileChannel) Files.newByteChannel(Paths.get("./nio/src/2.txt"))){
            ByteBuffer buffer = ByteBuffer.allocate(5);//指定缓冲区大小
            int count = seekableByteChannel.read(buffer);//将文件中的数据读取到缓冲区
            buffer.rewind();
            while (count > 0){
                System.out.println((char)buffer.get());//读取缓冲区中的数据
                count --;
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private static void read2()
    {
        try(FileChannel fileChannel = (FileChannel) Files.newByteChannel(Paths.get("./nio/src/2.txt"))){
            long size = fileChannel.size();//获取文件字节数量
            MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY,0,size);
            for(int i=0;i < size; i ++){
                System.out.println((char)mappedByteBuffer.get());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private static void write1()
    {
        try(FileChannel seekableByteChannel = (FileChannel) Files.newByteChannel(Paths.get("./nio/src/2.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE,StandardOpenOption.APPEND)){
            ByteBuffer buffer = ByteBuffer.allocate(5);//指定缓冲区大小
            for(int i=0;i<5;i++){
                buffer.put((byte)('A'+i));
            }
            buffer.rewind();
            seekableByteChannel.write(buffer);
        }catch (Exception e){
            e.printStackTrace();
        }


    }


    private static void write2()
    {
        Path path = Paths.get("./nio/src/4.txt");
        try(FileChannel fileChannel = (FileChannel) Files.newByteChannel(path,StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE)){
            MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,5);
            for(int i=0;i < 5; i ++){
                buffer.put((byte) ('A'+i));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void write3()
    {
        Path path = Paths.get("./nio/src/14.txt");
        try(FileChannel fileChannel = (FileChannel) Files.newByteChannel(path,StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE)){
            MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,1024);
            String str = "-今天天气很好啊";
            buffer.put(str.getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void copy()
    {
        Path path = Paths.get("./nio/src/4.txt");
        Path path2 = Paths.get("./nio/src/40.txt");
        try{
            Files.copy(path2,path, StandardCopyOption.REPLACE_EXISTING);
            //Files.move(path,path2, StandardCopyOption.REPLACE_EXISTING);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
